import { ITimer } from "../../api/timer/ITimer";
import { TimerUtils } from "../../api/timer/TimerUtils";
import { E_ArenaGlobal } from "../defs/arena/E_ArenaGlobal";
import { E_TimeConfig } from "../defs/com/E_TimeConfig";
import { E_PetBelongType } from "../defs/pet/E_PetBelongType";
import { CDArenaGlobal } from "../excel/CDArenaGlobal";
import { Player } from "../struct/Player";
import { SDActivity, SDActivityStatus } from "./SDActivity";
import { SDBattle } from "./SDBattle";
import { SDPlayer } from "./SDPlayer";


/**竞技场可挑战对象的最大个数 */
export const ARENA_ENEMY_MAX_NUM = 5;


/**
 * 数据-竞技场
 */
export abstract class SDArena {


    /**
     * 获取倒计时
     * @param player 
     * @param nowTime 
     * @returns  <= 0-可以刷新， >0需要的时间
     */
    public static getRefreshCountDown(
        player: Player,
        nowTime: number
    ): number {

        if (0 === player.arena.lastRefreshTime) {
            return 0;
        }

        const cds = CDArenaGlobal.getData(E_ArenaGlobal.FreshTime).value;
        const cd = E_TimeConfig.Second * cds;
        const delay = (player.arena.lastRefreshTime + cd) - nowTime;

        return delay > 0 ? delay : 0;
    }


    /**
     * 需要跨日更新
     * @param player 
     * @param nowTime 
     * @returns 
     */
    public static isNeedDailyUpdate(
        player: Player,
        nowTime = ITimer.getServerTime()
    ): boolean {

        const zt = TimerUtils.getDayZeroTime(nowTime);
        const lzt = TimerUtils.getDayZeroTime(player.arena.dailyFirstTime);
        if (zt !== lzt) {
            return true;
        }

        return false;
    }


    /**
    * 是否需要创建邮件
    * @param player 
    * @returns 
    */
    public static isNeedReqCreateMail(player: Player): boolean {
        const vs = player.arena.getRankRewardKeys();
        return vs.length > 0;
    }


    /**
    * 获取战力值
    * @param player 
    * @returns 
    */
    public static getFightScore(player: Player): number {

        const skillIdLevels = SDPlayer.getPlayerSkillIdLevels(player, []);
        const list = SDPlayer.getPlayerAttrs(player, skillIdLevels, [], E_PetBelongType.Arena);
        const score = SDBattle.getFightScore(list);

        return Math.floor(score);
    }


    /**
     * 获取当前赛季的开始时间
     * @param player 
     * @param nowTime 
     * @param getLast 当前没开取上一次开的时间
     */
    public static getCurrentArenaStartTime(
        player: Player,
        nowTime: number,
        getLast = true
    ): number {

        const activityId = CDArenaGlobal.getData(E_ArenaGlobal.ActivityID).value;
        const status = SDActivityStatus.createNew();
        SDActivity.getOpenStatus(player, activityId, nowTime, status);

        let time = 0;
        if (status.exist) {
            if (nowTime >= status.startTime && nowTime <= status.endTime) {
                time = status.startTime;
            }
        }

        //当前没有，取上一赛季的时间
        if (time === 0 && getLast) {
            const lt = SDActivity.getLastOpenTime(player, activityId, nowTime);
            if (lt >= 0) {
                time = lt;
            }
        }

        status.destroy();

        return time;
    }



}
